package com.hugedata.cdn.base.services;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import com.hugedata.cdn.base.filter.QueryFilter;
import com.hugedata.cdn.base.model.BaseModel;
import com.hugedata.cdn.base.util.Get32Primarykey;
import com.hugedata.cdn.util.CdnException;
import com.hugedata.cdn.util.ResultCode;

/**
 * 
 * 项目名称：xiaomifeng 类名称：MvasTransactionTemplate 类描述： 创建人：zhaoaihua 创建时间：2013-3-21
 * 下午5:32:00 修改备注：
 * 
 * @version
 * 
 */
public abstract class BaseServiceImpl extends SqlMapClientDaoSupport {

    protected static final String PRIMARY_KEY     = "ID";
    protected static final String FINDALL_PREFFIX = "findAll";
    protected static final String FINDALL_SUFFIX  = "List";
    protected static final String INSERT_PREFFIX  = "insert";
    protected static final String DELETE_PREFFIX  = "delete";
    protected static final String UPDATE_PREFFIX  = "update";
    protected static final String FIND_PREFFIX    = "find";
    protected static final String BYID_SUFFIX     = "ById";
    protected static final String BYFILTER_SUFFIX = "ListByFilter";
    protected final Log           log             = LogFactory
                                                          .getLog(getClass());

    public BaseServiceImpl() {}

    protected String getDomainName() {
        return null;
    }

    /**
     * 
     * queryForObject queryForObject封装
     * 
     * @param String
     *            statementName
     * @Object parameterObject
     * @return Object @ CdnException 晓密丰异常
     */
    protected Object queryForObject(String statementName, Object parameterObject)
            throws CdnException {

        try {
            return this.getSqlMapClientTemplate().queryForObject(statementName,
                    parameterObject);

        } catch (Exception ex) {

            throw new CdnException(ResultCode.DBError, ex);
        }

    }

    /**
     * 
     * queryForList queryForList封装
     * 
     * @param String
     *            statementName
     * @Object parameterObject
     * @return List @ CdnException 晓密丰异常
     */
    protected List<?> queryForList(String statementName, Object parameterObject)
            throws CdnException {

        try {
            return this.getSqlMapClientTemplate().queryForList(statementName,
                    parameterObject);

        } catch (Exception ex) {

            throw new CdnException(ResultCode.DBError, ex);
        }

    }

    /**
     * 
     * queryForList queryForList封装
     * 
     * @param String
     *            statementName
     * @return List @ CdnException 晓密丰异常
     */
    protected List<?> queryForList(String statementName) throws CdnException {

        try {
            return this.getSqlMapClientTemplate().queryForList(statementName);

        } catch (Exception ex) {

            throw new CdnException(ResultCode.DBError, ex);
        }

    }

    /**
     * 
     * insert insert封装
     * 
     * @param String
     *            statementName
     * @Object parameterObject
     * @return Object @ CdnException 晓密丰异常
     */
    protected Object insert(String statementName, Object parameterObject)
            throws CdnException {

        try {
            return this.getSqlMapClientTemplate().insert(statementName,
                    parameterObject);

        } catch (Exception ex) {

            throw new CdnException(ResultCode.DBError, ex);
        }

    }

    /**
     * 
     * update update封装
     * 
     * @param String
     *            statementName
     * @Object parameterObject
     * @return int @ CdnException 晓密丰异常
     */
    protected int update(String statementName, Object parameterObject)
            throws CdnException {

        try {
            return this.getSqlMapClientTemplate().update(statementName,
                    parameterObject);

        } catch (Exception ex) {

            throw new CdnException(ResultCode.DBError, ex);
        }

    }

    /**
     * 
     * update update封装
     * 
     * @param String
     *            statementName
     * @Object parameterObject
     * @return int @ CdnException 晓密丰异常
     */
    protected int update(String statementName) throws CdnException {

        try {
            return this.getSqlMapClientTemplate().update(statementName);

        } catch (Exception ex) {

            throw new CdnException(ResultCode.DBError, ex);
        }

    }

    /**
     * 
     * delete delete封装
     * 
     * @param String
     *            statementName
     * @Object parameterObject
     * @return int @ CdnException 晓密丰异常
     */
    protected int delete(String statementName, Object parameterObject)
            throws CdnException {

        try {
            return this.getSqlMapClientTemplate().delete(statementName,
                    parameterObject);

        } catch (Exception ex) {

            throw new CdnException(ResultCode.DBError, ex);
        }

    }

    public void deleteEntityById(String id) throws Exception {
        getSqlMapClientTemplate().delete(getDeleteStatementId(), id);
    }

    public BaseModel findEntityById(String id) {
        return (BaseModel) getSqlMapClientTemplate().queryForObject(
                getFindbyStatementId(), id);
    }

    public List findEntityListByFilter(QueryFilter filter) {
        return getSqlMapClientTemplate().queryForList(
                getFindbyfilterStatementId(), filter);
    }

    public String insertEntity(BaseModel entity) throws Exception {
        entity.setId(Get32Primarykey.getRandom32BeginTimePK());
        if (entity.getCreateDatetime() == null) {
            entity.setCreateDatetime(new Date());
        }
        getSqlMapClientTemplate().insert(getInsertStatementId(), entity);
        return entity.getId();
    }

    public void updateEntity(BaseModel entity) throws Exception {
        if (entity.getUpdateDatetime() == null) {
            entity.setCreateDatetime(new Date());
        }
        getSqlMapClientTemplate().update(getUpdateStatementId(), entity);
    }

    public List findAllEntityList() {
        return getSqlMapClientTemplate().queryForList(getFindAllStatementId());
    }

    protected String getDeleteStatementId() {
        if (StringUtils.isEmpty(getDomainName())) {
            log.error("Currently no domain name specified in your ServiceImpl class which inherited from BaseServiceImpl");
            log.error("You have to implement method getDomainName() in order to execute sql");
        }
        return "delete" + getDomainName() + "ById";
    }

    protected String getInsertStatementId() {
        if (StringUtils.isEmpty(getDomainName())) {
            log.error("Currently no domain name specified in your ServiceImpl class which inherited from BaseServiceImpl");
            log.error("You have to implement method getDomainName() in order to execute sql");
        }
        return "insert" + getDomainName();
    }

    protected String getUpdateStatementId() {
        if (StringUtils.isEmpty(getDomainName())) {
            log.error("Currently no domain name specified in your ServiceImpl class which inherited from BaseServiceImpl");
            log.error("You have to implement method getDomainName() in order to execute sql");
        }
        return "update" + getDomainName() + "ById";
    }

    protected String getFindbyStatementId() {
        if (StringUtils.isEmpty(getDomainName())) {
            log.error("Currently no domain name specified in your ServiceImpl class which inherited from BaseServiceImpl");
            log.error("You have to implement method getDomainName() in order to execute sql");
        }
        return "find" + getDomainName() + "ById";
    }

    protected String getFindbyfilterStatementId() {
        if (StringUtils.isEmpty(getDomainName())) {
            log.error("Currently no domain name specified in your ServiceImpl class which inherited from BaseServiceImpl");
            log.error("You have to implement method getDomainName() in order to execute sql");
        }
        return "find" + getDomainName() + "ListByFilter";
    }

    protected String getFindAllStatementId() {
        if (StringUtils.isEmpty(getDomainName())) {
            log.error("Currently no domain name specified in your ServiceImpl class which inherited from BaseServiceImpl");
            log.error("You have to implement method getDomainName() in order to execute sql");
        }
        return "findAll" + getDomainName() + "List";
    }
}
